//
//  XXIncomeTaxCalculationTextCell.m
//  XingXue
//
//  Created by XingXue Team on 2023.
//  Copyright © 2023 XingXue Team. All rights reserved.
//

#import "XXIncomeTaxCalculationTextCell.h"
#import <objc/runtime.h>

@implementation XXIncomeTaxCalculationTextCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    _textField.delegate = self;

    Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *minPlaceholderLabel = object_getIvar(_textField, ivar);
    minPlaceholderLabel.textColor = UIColorWithHex(0x999999);
    minPlaceholderLabel.font = [UIFont boldSystemFontOfSize:14];
    
    _textView.layer.borderColor = UIColorFromHex(0x375BEB).CGColor;
    _textView.layer.borderWidth = 0.5;
}

- (void)setData:(id)data delegate:(id)delegate {
    
    _delegate = delegate;
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        if ([data[@"data"] isKindOfClass:[XXIncomeTaxCalculationTextCellModel class]]) {
            XXIncomeTaxCalculationTextCellModel *model = data[@"data"];
            _model = model;

            _titleLabel.text = model.title;
            _textField.text = model.content;
            _textField.placeholder = model.tip;
            
            
            if (model.position == 0) {
                _viewTop.constant = 0;
                _viewBottom.constant = -15;
                _lineLabel.hidden = NO;
            } else if (model.position == 1) {
                _viewTop.constant = -15;
                _viewBottom.constant = -15;
                _lineLabel.hidden = NO;
            } else if (model.position == 2) {
                _viewTop.constant = -15;
                _viewBottom.constant = 0;
                _lineLabel.hidden = YES;
            }
            
            _textField.keyboardType = UIKeyboardTypeNumberPad;
        }
    }
}

- (IBAction)textFieldChange:(UITextField *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(incomeTaxCalculationTextCell:textField:)]) {
        [_delegate incomeTaxCalculationTextCell:self textField:sender];
    }
}

- (IBAction)textFieldBegin:(UITextField *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(incomeTaxCalculationTextCell:textField:)]) {
        [_delegate incomeTaxCalculationTextCell:self textField:sender];
    }
}

@end


#pragma mark - Model

@implementation XXIncomeTaxCalculationTextCellModel

+ (XXIncomeTaxCalculationTextCellModel *)ittemModelWithTitle:(NSString *)title content:(NSString *)content tip:(NSString *)tip position:(NSInteger)position {
    XXIncomeTaxCalculationTextCellModel *model = [XXIncomeTaxCalculationTextCellModel new];
    model.title = title.length > 0 ? title : @"";
    model.content = content.length > 0 ? content : @"";
    model.tip = tip.length > 0 ? tip : @"";
    model.position = position;
    return model;
}

@end
